home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Demos
/
A.D. Software
/
OOFILE
/
Buildable, limited OOFILE
/
samples
/
ooftst20.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1996-03-21
|
3KB
|
126 lines
// Copyright 1994 A.D. Software. All Rights Reserved
// OOFTEST20
// This sample demonstrates how to create an ad-hoc query.
// Simple stream I/O is used to interact with the user.
#include "oofile.hpp"
#include "ooftst01.inc"
void anyName(const char* searchStrings[], bool isOr, unsigned short numSch);
void lastName(const char* searchStrings[], unsigned short numSch);
#define numEntries(A) sizeof(A)/sizeof(A[0])
dbConnect_ctree theDB;
dbPeople People;
int main()
{
cout << "OOFILE Validation Suite - Test 20\n"
<< "Demonstration of ad-hoc query building\n";
theDB.useSeparateFiles(); // note the blank connection names!
// this test creates People.dat, People.idx & Blobs
#ifdef _Macintosh
// this feature only on the Mac at present
#define kExistsName ":test01:People.dat"
#define kDatabaseName "test01"
#else
#define kExistsName "People.dat"
#define kDatabaseName ""
#endif
if (dbConnect::fileExists(kExistsName))
theDB.openConnection(kDatabaseName);
else {
theDB.newConnection(kDatabaseName);
People.AddTestData();
}
People.selectAll();
cout << "Listing records\n" << (dbView(People) << People.LastName << People.OtherNames);
const char* userEntries[] = {"Dent", "Andy"};
anyName(userEntries, true, numEntries(userEntries));
anyName(userEntries, false, numEntries(userEntries));
const char* userEntries2[] = {"Dent", "Tay*"};
lastName(userEntries2, numEntries(userEntries));
cout << "Test Completed" << endl;
return EXIT_SUCCESS;
}
void
anyName(const char* searchStrings[], bool isOr, unsigned short numSch)
{
dbQuery theQuery;
if (isOr)
cout << "OR";
else
cout << "AND";
cout << " Querying either Last or Othernames = \n";
for (unsigned short i=0; i<numSch; i++) {
cout << " " << searchStrings[i] << endl;
// construct clauses on the heap otherwise they are temporaries
// which won't exist by the time we do the search()
dbQueryClause* term1 = new dbQueryBinary(
People.LastName == searchStrings[i]
);
dbQueryClause* term2 = new dbQueryBinary(
People.OtherNames == searchStrings[i]
);
// the following is a bit subtle - the operator||
// which takes pointers creates a dbQueryBinaryComboOwner
// which will delete the sub-clauses
if (isOr) {
// theQuery |= (*term1 || term2); equivalent to the following
theQuery |= term1;
theQuery |= term2;
}
else
theQuery &= (*term1 || term2);
}
People.search(theQuery);
cout << "\nListing result of query\n"
<< (dbView(People) << People.LastName << People.OtherNames) << endl;
}
void
lastName(const char* searchStrings[], unsigned short numSch)
{
dbQuery theQuery;
cout << "Querying Lastname = any of\n";
for (unsigned short i=0; i<numSch; i++) {
cout << " " << searchStrings[i] << endl;
// construct clauses on the heap otherwise they are temporaries
// which won't exist by the time we do the search()
dbQueryClause* theTerm = new dbQueryBinary(
People.LastName == searchStrings[i]
);
theQuery |= theTerm;
}
People.search(theQuery);
cout << "\nListing result of query\n"
<< (dbView(People) << People.LastName << People.OtherNames) << endl;
}